/*******************************************************************************
* File Name: motor_ctrl.h
* Version 1.00
*
* Description:
*  This file provides the source code to the API for the Sequencing Successive
*  Approximation ADC Component Component.
*
* Note:
*
********************************************************************************
* Copyright 2010-2019, CMS Semiconductor Corporation.  All rights reserved.
* You may use this file only in accordance with the license, terms, conditions,
* disclaimers, and limitations in the end user license agreement accompanying
* the software package with which this file was provided.
*******************************************************************************/

#ifndef __MOTOR_CTRL_H__
#define __MOTOR_CTRL_H__

#ifdef __cplusplus
extern "C"
{
#endif

/*****************************************************************************/
/* Include files */
/*****************************************************************************/
#include<stdint.h>

/*****************************************************************************/
/* Global pre-processor symbols/macros ('#define') */
/*****************************************************************************/

						
/*****************************************************************************/
/* Global type definitions ('typedef') */
/*****************************************************************************/
typedef struct {
	volatile uint8_t Motor_State;
	volatile uint16_t Fault_Type;        
	volatile int32_t Q8_Vbus;
	uint32_t DeltaThetaTs;
	int32_t DeltaThetaKTs;
	int32_t Rotor_ElectricAngle;
	int32_t AngleError;
	uint8_t Running_level;
	uint8_t Closedloop_f;
	uint8_t ChangeSpeed_EN;
	uint8_t ChangePiPar_EN;
	uint8_t SpeedPiMode;
} stc_motor_run_t;

typedef struct {
	uint8_t IdReg_EN;		//Id PI Enable
	uint8_t IqReg_EN;		//Iq PI Enable
	uint8_t SpdReg_EN;	//speed PI Enable
	uint8_t FdWk_EN;	  	//field weaken PI Enable

	uint16_t SpdPICyc;	//execute cycle of speed PI
	uint16_t FdWkPICyc;	//execute cycle of field weaken PI
	
	uint16_t SpdPICnt;	//counter for speed PI	
	uint16_t FdWkPICnt;	//counter for field weaken PI	
} stc_pidCtrl_t;

typedef struct {
	int32_t  Q0_CommandSpd_rpm;
	uint32_t Q0_CommandSpd_rpm_Max;
	uint32_t Q0_CommandSpd_rpm_Min;
	uint32_t Q20_CommandSpd_Hz;
	uint32_t Q0_TargetSpd_rpm;
	uint32_t Q8_TargetSpd_Hz;
	uint32_t Q20_TargetSpd_Hz;
	uint32_t Q20_TargetSpd_IncTs;
	uint32_t Q20_TargetSpd_DecTs;
	int32_t Q8_MotorRTS_Hz;
	int32_t Q8_MotorRTS_Hz_Lpf;
	uint8_t Spd_Regulate_EN;
} stc_spdCtrl_t;

typedef struct {
	volatile unsigned long Timer0;
	volatile unsigned long Timer1;
	volatile unsigned long Timer2;
	volatile unsigned long Timer3;
} stc_soft_timer_t;

/*****************************************************************************/
/* Global variable declarations ('extern', definition in C source) */
/*****************************************************************************/


/*****************************************************************************/
/* Global function prototypes ('extern', definition in C source) */
/*****************************************************************************/
extern void Motor0_Init(unsigned short Sample_freq);

extern void Motor0_Start(void);

extern void Motor_Stop(void);

#ifdef __cplusplus
}
#endif

#endif /* __MOTOR_CTRL_H__ */

